package com.products.utils;

import com.products.model.Product;
import java.util.List;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.activation.*;
import java.util.Properties;


public class SimpleMail {

    private static final String SMTP_HOST_NAME = "smtp.mail.ru";
    private static final String SMTP_AUTH_USER = "usr_name";
    private static final String SMTP_AUTH_PWD  = "usr_pass";

    private static final String SMTP_SENDER        = "";
    private static final String SMTP_FR            = "";
    private static final String SMTP_RECIEVER      = "";
    private static final String SMTP_TO            = "";
    private static final String SMTP_SUBJECT       = "Products From Office";
    private static final String SMTP_FILE_HTMLTEXT = "";
    private static final String SMTP_FILE_ATTACH   = "";

    private static final String SMTP_BODY_HTMLTEXT = "";
    
    public static int Send(List<Product> products,
                           String SMTP_SENDER,
                           String SMTP_FR,
                           String SMTP_RECIEVER,
                           String SMTP_TO,
                           String SMTP_SUBJECT,
                           String SMTP_FILE_HTMLTEXT
                           ) throws Exception{
        Properties props = new Properties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.host", SMTP_HOST_NAME);
        props.put("mail.smtp.auth", "true");
        
        InternetAddress props_from = new InternetAddress();
        InternetAddress props_to = new InternetAddress();

        props_from.setPersonal(SMTP_SENDER);
        props_from.setAddress(SMTP_FR);
        props_to.setPersonal(SMTP_RECIEVER);
        props_to.setAddress(SMTP_TO);

        Authenticator auth = new SMTPAuthenticator();
        Session mailSession = Session.getDefaultInstance(props, auth);
        // uncomment for debugging infos to stdout
        // mailSession.setDebug(true);        
        Transport transport = mailSession.getTransport();

        MimeMessage message = new MimeMessage(mailSession);
        
        message.setFrom(props_from); // От кого
        message.addRecipient(Message.RecipientType.TO, props_to); // Кому
        message.setSubject(SMTP_SUBJECT); // Тема 

        // Файл с телом сообщения
        FileDataSource source = new FileDataSource(SMTP_FILE_HTMLTEXT);        
        DataHandler dhandler = new DataHandler(source);
        //message.setContent(buildMailMessage(products), "text/html;charset=windows-1251"); // Тело сообщения
        message.setContent(dhandler.getContent(), "text/html;charset=windows-1251"); // Тело сообщения
        
        transport.connect(); // Подключение
        transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); // Отправка
        transport.close(); // Закрытие сессии

        return 0;
    }
   
    private static class SMTPAuthenticator extends javax.mail.Authenticator {
        @Override
        public PasswordAuthentication getPasswordAuthentication() {
           String username = SMTP_AUTH_USER;
           String password = SMTP_AUTH_PWD;
           return new PasswordAuthentication(username, password);
        }
    }
    private String getContent(List<Product> products){
            StringBuilder buffer = new StringBuilder();
            for (int i=0; i<products.size(); i++){
                    Product product = products.get(i);
                    buffer.append(product.toString()).append("\n");
            }
            System.out.println("buffer.toString()="+buffer.toString());
            if (buffer.toString().trim().length()==0){
                    return "Order is Empty.";
            }
            return buffer.toString();
    }
}